{% extends "layout.html" %}
{% set active_page = "upgrade" %}
{% block title %} - Upgrade{% endblock %}

{% block head %}
{% if upgrade == 1 %}
<script>
  let prev_string = '';
  function check_upgrade_status() {
    const url = '/admin/upgrade_status';
    $.ajax(url, {
      success: function(new_string, responseText, jqXHR) {
        if (jqXHR.status !== 204) {
          if (new_string !== prev_string) {
            prev_string = new_string;
            document.getElementById("upgrade_status").innerHTML = new_string;
            let objDiv = document.getElementById("upgrade_status");
            objDiv.scrollTop = objDiv.scrollHeight;
          }
        }
        else {
          document.getElementById("upgrade_status").innerHTML = "Attempting to read the mycodo upgrade log. Please wait...";
        }
      },
      cache: false
    });
  }
  check_upgrade_status();
  setInterval(function() {
      check_upgrade_status();
    }, 5000
  );
</script>
{% endif %}
{% endblock %}

{% block body %}
  <!-- Route: /admin/upgrade -->
  <div class="container">
    {% include 'flash_messages.html' %}

    {% if not is_internet %}

    <div style="padding: 0.5em 0">
      {{_('No internet connection detected. To upgrade Mycodo automatically, you will need an internet connection. Refresh the page when one is connected.')}} The settings for detecting the presence of an internet connection can be found on the Upgrade Options section of the Configuration page.
    </div>

    {% elif upgrade == 1 %}

    <div style="padding: 0.5em 0">
      <p>{{_('Mycodo is currently in the process of an upgrade')}}</p>
      <p>{{_('If an upgrade is not currently in progress, you may delete the /opt/Mycodo/.upgrade file to be able to reinitiate the upgrade process.')}}</p>
      <p>{{_('An upgrade often takes several minutes, and in some cases longer, to complete. Give the process ample time to run before doing anything. When the upgrade has successfully finished, the daemon status indicator at the top left will change from red to green. Some drivers require the backend to be restarted for them to become active. If you experience an erorr, attempt to restart the backend before creating a new github issue.')}}</p>
      <p>{{_('You can monitor the upgrade progress with the live log view, below, by navigating to Configure -> Mycodo Logs -> Upgrade Log, or by opening /var/log/mycodo/mycodoupgrade.log. Below is the upgrade log, updated every 5 seconds. If an error occurs, it is advisable to inspect the entire log in order to diagnose the issue.')}}</p>
    </div>
    <pre style="overflow:auto; resize: vertical; height:400px" id="upgrade_status"></pre>

    {% elif current_latest_major_version != current_release.split('.')[0] and
          current_release in final_releases and
          current_release != current_latest_release and
          current_latest_release != '0.0.0' and
          upgrade != 1 %}

    <p style="padding-top: 0.5em">
      Installed version: <a href="https://github.com/kizniche/Mycodo/releases/tag/v{{current_release}}" target="_blank">{{current_release}}</a>
      <br>Latest version: <a href="https://github.com/kizniche/Mycodo/releases/tag/v{{current_latest_release}}" target="_blank">{{current_latest_release}}</a>
    </p>
    <p>
      Refer to the <a href="https://github.com/kizniche/Mycodo/blob/master/CHANGELOG.md" target="_blank">Changelog</a> for a full list of changes.
      <br>Major version changes: <a href="https://github.com/kizniche/Mycodo/releases/tag/v{{current_latest_major_version}}.0.0" target="_blank">{{current_latest_major_version}}.0.0</a>
    </p>
    <p style="font-weight: bold; color: red">Major version change detected!</p>
    <p>A new major version has been released on GitHub: v{{current_latest_major_version}}.x.x (currently at v{{current_latest_release}}). This usually means there are system-breaking changes for some or all users, and if you choose to upgrade, your system settings may not be able to be preserved. It is advisable to backup these settings by either <a href="/export">exporting</a> the settings database (/opt/Mycodo/databases/mycodo.db) or making note of important information (GPIO pins, PID settings, etc.).</p>
    <p>However, there may be cases where the settings database can be preserved during the upgrade. There may be other cases that an upgrade is not possible, even if the settings database is wiped. This will be dependent on both your currently installed version and the current version available for upgrade and won't be known until an upgrade is attempted. The upgrade script will inform you of any of these situations after the upgrade is initiated.</p>
    <p>If you would like to attempt to upgrade Mycodo to v{{current_latest_release}}, select the button below.</p>
    <div class="form-inline" style="padding-bottom: 1em">
      <form method="post" action="/admin/upgrade">
        <input type="hidden" name="form-name" value="upgrade">
        {{form_upgrade.csrf_token}}
        <div>
          <div class="form-group">
            {{form_upgrade.upgrade_next_major_version(class_='btn btn-primary', **{'onclick':'return confirm("Your settings database may not be preserved if you choose to upgrade. Confirm that you really want to do this.")'})}}
          </div>
        </div>
      </form>
    </div>

    {% elif upgrade == 2 %}

    <div style="padding: 0.5em 0">
      <p>{{_('There was an error that occurred during the upgrade')}}</p>
      <p>{{_('View the upgrade log at /var/log/mycodo/mycodoupgrade.log.')}}</p>
      <p>{{_('Delete /opt/Mycodo/.upgrade to acknowledge this message and restore upgrade functionality.')}}</p>
    </div>

    {% elif force_upgrade_master %}

    <div style="padding: 1em 0 1.5em 0">
      <p>MANUAL UPGRADE MODE</p>
      <p>FORCE_UPGRADE_MASTER has been set to True in /opt/Mycodo/config.py.</p>
      <p>Pressing upgrade below will perform an upgrade from the <a href="https://github.com/kizniche/Mycodo/archive/master.tar.gz">master.tar.gz</a> archive of the <a href="https://github.com/kizniche/Mycodo/tree/master">Mycodo master branch</a> instead of the Release version.</p>
      <p>If you would like to upgrade to the latest Release version instead, set <code>FORCE_UPGRADE_MASTER = False</code> in /opt/Mycodo/mycodo/config.py, restart the web UI with the <code>sudo service mycodoflask restart</code> command, then refresh this page.</p>
    </div>
    <div class="form-inline" style="padding-bottom: 1em">
      <form method="post" action="/admin/upgrade">
        <input type="hidden" name="form-name" value="upgrade">
        {{form_upgrade.csrf_token}}
        <div>
          <div class="form-group">
            {{form_upgrade.upgrade(class_='btn btn-primary')}}
          </div>
        </div>
      </form>
    </div>

    {% elif not upgrade_available %}

    <div style="padding: 0.5em 0">
      {{_('No upgrade is available. You are running the latest release, version')}} <a href="https://github.com/kizniche/Mycodo/releases/tag/v{{current_release}}" target="_blank">{{ current_release }}</a>
    </div>

    {% elif upgrade_available %}

    <div style="padding: 0.5em 0">
      Installed version: <a href="https://github.com/kizniche/Mycodo/releases/tag/v{{current_release}}" target="_blank">{{current_release}}</a>
      <br>Latest version: <a href="https://github.com/kizniche/Mycodo/releases/tag/v{{current_major_release}}" target="_blank">{{current_major_release}}</a>
    </div>
    <div style="padding: 0.5em 0">
      You are {{releases_behind}} release{% if releases_behind is not none and releases_behind > 1 %}s{% endif %} behind the latest version.
      <br>Refer to the <a href="https://github.com/kizniche/Mycodo/blob/master/CHANGELOG.md" target="_blank">Changelog</a> for a list of all changes.
    </div>
    <div style="padding: 1em 0 1.5em 0">
      {{_('Click the button below to begin the upgrade process. Note: An upgrade often takes several minutes, and in rare cases hours, to complete. During that time, the Mycodo web interface and server recording sensors and manipulating outputs (among other services) will be completely shut down. It is not recommended not perform an upgrade on a production machine (i.e. trust buy verify: treat the new version as unstable until you have verified it works as expected).')}}
    </div>
    <div class="form-inline" style="padding-bottom: 1em">
      <form method="post" action="/admin/upgrade">
        <input type="hidden" name="form-name" value="upgrade">
        {{form_upgrade.csrf_token}}
        <div>
          <div class="form-group">
            {{form_upgrade.upgrade(class_='btn btn-primary')}}
          </div>
        </div>
      </form>
    </div>

      {% if releases_behind is not none and releases_behind > 1 %}

    <div style="font-family: 'Courier New', monospace;">
      <table class="table" style="width: auto">
        <tr>
          <th>{{_('Current to latest versions')}}</th>
        </tr>
        {% for each_release in current_releases %}
          <tr>
            <td>
              <a href="https://github.com/kizniche/Mycodo/releases/tag/v{{ each_release }}" target="_blank" {% if current_release == each_release %}style="color: red"{% endif %}>v{{ each_release }}</a>
            </td>
            <td>
              {% if each_release == current_release %}{{_('current')}}{% elif each_release == current_major_release %}{{_('latest')}}{% endif %}
            </td>
          </tr>
        {% endfor %}
      </table>
    </div>

      {% endif %}

    {% endif %}
  </div>

{% endblock %}
